﻿using System;
using System.Collections.Generic;
using System.Data;
using BLL;
using Utils;
namespace UI.ModSalary
{
    public partial class JJAddNormal : System.Web.UI.Page
    {
        JJBiz jjBiz=new JJBiz();
        EmployeeBiz employeeBiz = new EmployeeBiz();
        DefDicBiz defDicBiz = new DefDicBiz();
        JjProcBiz jjProcBiz = new JjProcBiz();
        JjProdBiz jjProdBiz = new JjProdBiz();
        string code = string.Empty;
        string account = "";
        //int deptID = 0;
        //int roleID = 0;
        //string isSpecal = "N";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
                //deptID = user.DepartemntID;
                //isSpecal = user.IsSpecial;
                //roleID = user.RoleID;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (Request.Params["JJCode"] != null)
            {
                code = Request.Params["JJCode"].ToString();
                btnAdd.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            if(!IsPostBack)
            {
               txtJJCode.Text= defDicBiz.CreateCode("S", "Wage_Jj", 3,"dd");
             
               if (Request.Cookies["jjtime"] != null)
               {
                   txtJJTime.Text = Request.Cookies["jjtime"].Value;
               } 
               else                   
               txtJJTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
               DataTable dt0 = new JjProdBiz().GetLists();
               defDicBiz.InitDropDownList(dt0, ddlProCode);
               if (!string.IsNullOrEmpty(code))
               { BindData(code);
               btnUpdate.Enabled = true;
               btnDelete.Enabled = true;
               }
            }
        }
        private void BindData(string code)
        {
            DataTable dt = jjBiz.GetOneInfo(code);
            if (dt.Rows.Count > 0)
            {
                txtJJCode.Text = code;
                string jjTime = dt.Rows[0]["JJTime"].ToString();
                if (Utils.RegexRule.IsDate(jjTime))
                    txtJJTime.Text = Convert.ToDateTime(jjTime).ToString("yyyy-MM-dd");
             string productCode=dt.Rows[0]["ProCode"].ToString();
             defDicBiz.InitSynDropDownList2(ddlProCode, productCode);
               DataTable dt1 = jjProcBiz.GetLists(productCode, "Y");
               defDicBiz.InitDropDownList(dt1, ddlProcessCode, dt.Rows[0]["ProcessName"].ToString());

                
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                lbEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                lbDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                lbDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                lbProName.Text = dt.Rows[0]["ProName"].ToString();

                lbProcName.Text = dt.Rows[0]["ProcessName"].ToString();
                txtPrice.Text = dt.Rows[0]["ProcessPrice"].ToString();
                txtScrapSal.Text = dt.Rows[0]["ScrapPrice"].ToString();
                txtNum.Text = dt.Rows[0]["Num"].ToString();
                txtNumAmount.Text = dt.Rows[0]["NumAmount"].ToString();
                txtScrapNum.Text = dt.Rows[0]["ScrapNum"].ToString();
                txtScrapAmount.Text = dt.Rows[0]["ScrapAmount"].ToString();
                txtJjSalary.Text = dt.Rows[0]["JjSalary"].ToString();
                txtMemo.Text = dt.Rows[0]["Memo"].ToString();
            }

        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (!AccountBiz.CheckRight(account, "JJAddNormal.aspx", RightChar.Add))
                {
                    if (!AccountBiz.CheckRight(account, "JJNormalList.aspx", RightChar.Add))
                    {
                        JSUtility.Alert(this, "您没有增加的权限！");
                        return;
                    }
                }
                string jjCode = txtJJCode.Text.Trim();
                if (string.IsNullOrEmpty(jjCode))
                {
                    JSUtility.Alert(this, "传票号不能为空"); return;
                }
                if (jjBiz.IsExist(jjCode))
                {
                    JSUtility.Alert(this, "传票号已经存在"); return;
                }
                string jjTime = txtJJTime.Text.Trim();
                if (string.IsNullOrEmpty(jjTime))
                {
                    JSUtility.Alert(this, "时间不能为空"); return;
                }
                if (!Utils.RegexRule.IsDate(jjTime))
                { JSUtility.Alert(this, "时间格式不对"); return; }
               
                    Response.Cookies["jjtime"].Value = jjTime;
                    Response.Cookies["jjtime"].Expires = DateTime.Now.AddDays(1);                

                string empCode = txtEmpCode.Text.Trim();
                string empName = lbEmpName.Text;
                string deptName = lbDeptName.Text;
                string dutyName = lbDutyName.Text;
                if (string.IsNullOrEmpty(empCode) || string.IsNullOrEmpty(empName))
                { JSUtility.Alert(this, "工号或名字不能为空"); return; }


                string proCode = ddlProCode.SelectedValue;
                string proName = lbProName.Text;
                //if (proCode == "0" || string.IsNullOrEmpty(proName))
                //{ JSUtility.Alert(this, "产品编号或产品名称必须填写"); return; }

                string procCode = ddlProcessCode.SelectedValue;
                string procName = lbProcName.Text;
                //if (procCode == "0" || string.IsNullOrEmpty(procName))
                //{ JSUtility.Alert(this, "工序编号或工序名称必须填写"); return; }

                string price = txtPrice.Text.Trim();
                string scrapSal = txtScrapSal.Text.Trim();
                string num = txtNum.Text.Trim();
                string numAmount = txtNumAmount.Text.Trim();
                string scrapNum = txtScrapNum.Text.Trim();
                string scrapAmount = txtScrapAmount.Text.Trim();
                string jjSalary = txtJjSalary.Text.Trim();
                if (!RegexRule.IsNumeric(price) || !RegexRule.IsNumeric(scrapSal) || !RegexRule.IsNumeric(num)
                    || !RegexRule.IsNumeric(numAmount) || !RegexRule.IsNumeric(scrapNum) || !RegexRule.IsNumeric(scrapAmount)
                    || !RegexRule.IsNumeric(jjSalary))
                { JSUtility.Alert(this, "数字填写错误！"); return; }
                string memo = txtMemo.Text.Trim();
                SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
                sdList.Add("JJCode", jjCode);
                sdList.Add("JJTime", jjTime);
                sdList.Add("EmpCode", empCode);
                sdList.Add("EmpName", empName);
                sdList.Add("DeptName", deptName);
                sdList.Add("DutyName", dutyName);

                sdList.Add("ProCode", proCode);
                sdList.Add("ProName", proName);

                sdList.Add("ProcessCode", procCode);
                sdList.Add("ProcessName", procName);
                sdList.Add("ProcessPrice", price);
                sdList.Add("ScrapPrice", scrapSal);

                sdList.Add("Num", num);
                sdList.Add("NumAmount", numAmount);
                sdList.Add("ScrapNum", scrapNum);
                sdList.Add("ScrapAmount", scrapAmount);

                sdList.Add("JjSalary", jjSalary);              
                sdList.Add("Account", account);
                sdList.Add("Memo", memo);
                string info = "";
                if (jjBiz.Insert(sdList))
                    info = "增加信息成功";
                else
                    info = "增加信息失败";
                JSUtility.AlertAndRedirect(info, "JJNormalAdd.aspx");
            }
            catch(Exception ee)
            {
                JSUtility.Alert(this, ee.Message);
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (!AccountBiz.CheckRight(account, "JJAddNormal.aspx", RightChar.Mod))
                {
                    if (!AccountBiz.CheckRight(account, "JJNormalList.aspx", RightChar.Mod))
                    {
                        JSUtility.Alert(this, "您没有修改的权限！");
                        return;
                    }
                }
                string jjCode = txtJJCode.Text.Trim();
                if (string.IsNullOrEmpty(jjCode))
                {
                    JSUtility.Alert(this, "传票号不能为空"); return;
                }
                if (!jjBiz.IsExist(jjCode))
                {
                    JSUtility.Alert(this, "传票号不存在"); return;
                }
                string jjTime = txtJJTime.Text.Trim();
                if (string.IsNullOrEmpty(jjTime))
                {
                    JSUtility.Alert(this, "时间不能为空"); return;
                }
                if (!Utils.RegexRule.IsDate(jjTime))
                { JSUtility.Alert(this, "时间格式不对"); return; }

                string empCode = txtEmpCode.Text.Trim();
                string empName = lbEmpName.Text;
                string deptName = lbDeptName.Text;
                string dutyName = lbDutyName.Text;
                if (string.IsNullOrEmpty(empCode) || string.IsNullOrEmpty(empName))
                { JSUtility.Alert(this, "工号或名字不能为空"); return; }


                string proCode = ddlProCode.SelectedValue;
                string proName = lbProName.Text;
                //if (proCode == "0" || string.IsNullOrEmpty(proName))
                //{ JSUtility.Alert(this, "产品编号或产品名称必须填写"); return; }

                string procCode = ddlProcessCode.SelectedValue;
                string procName = lbProcName.Text;
                //if (procCode == "0" || string.IsNullOrEmpty(procName))
                //{ JSUtility.Alert(this, "工序编号或工序名称必须填写"); return; }

                string price = txtPrice.Text.Trim();
                string scrapSal = txtScrapSal.Text.Trim();
                string num = txtNum.Text.Trim();
                string numAmount = txtNumAmount.Text.Trim();
                string scrapNum = txtScrapNum.Text.Trim();
                string scrapAmount = txtScrapAmount.Text.Trim();
                string jjSalary = txtJjSalary.Text.Trim();
                if (!RegexRule.IsNumeric(price) || !RegexRule.IsNumeric(scrapSal) || !RegexRule.IsNumeric(num)
                    || !RegexRule.IsNumeric(numAmount) || !RegexRule.IsNumeric(scrapNum) || !RegexRule.IsNumeric(scrapAmount)
                    || !RegexRule.IsNumeric(jjSalary))
                { JSUtility.Alert(this, "数字填写错误！"); return; }
                string memo = txtMemo.Text.Trim();
                SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
                sdList.Add("JJCode", jjCode);
                sdList.Add("JJTime", jjTime);
                sdList.Add("EmpCode", empCode);
                sdList.Add("EmpName", empName);
                sdList.Add("DeptName", deptName);
                sdList.Add("DutyName", dutyName);

                sdList.Add("ProCode", proCode);
                sdList.Add("ProName", proName);

                sdList.Add("ProcessCode", procCode);
                sdList.Add("ProcessName", procName);
                sdList.Add("ProcessPrice", price);
                sdList.Add("ScrapPrice", scrapSal);

                sdList.Add("Num", num);
                sdList.Add("NumAmount", numAmount);
                sdList.Add("ScrapNum", scrapNum);
                sdList.Add("ScrapAmount", scrapAmount);

                sdList.Add("JjSalary", jjSalary);
                sdList.Add("Account", account);
                sdList.Add("Memo", memo);
                string info = "";
                if (jjBiz.Update(sdList))
                    info = "更新信息成功";
                else
                    info = "更新信息失败";
                JSUtility.Alert(this, info);
            }
            catch (Exception ee)
            {
                JSUtility.Alert(this, ee.Message);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "JJAddNormal.aspx", RightChar.Del))
            {
                if (!AccountBiz.CheckRight(account, "JJNormalList.aspx", RightChar.Del))
                {
                    JSUtility.Alert(this, "您没有删除的权限！");
                    return;
                }
            }
            string jjCode = txtJJCode.Text.Trim();
            if (string.IsNullOrEmpty(jjCode))
            {
                JSUtility.Alert(this, "传票号不能为空"); return;
            }
            if (!jjBiz.IsExist(jjCode))
            {
                JSUtility.Alert(this, "传票号不存在"); return;
            }

            string info = string.Empty;
            if (jjBiz.Delete(jjCode))
                info = "增加信息成功";
            else
                info = "增加信息失败";
            JSUtility.Alert(this, info);
        }

        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (Utils.StringUtility.IncludeHZ(empCode))//如果包含汉字
                {
                    DataTable dt = employeeBiz.GetOneInfoByName(empCode);
                    if (dt.Rows.Count == 0)
                    {
                        JSUtility.Alert(this, "不存在该员工姓名: " + empCode);
                    }
                    else if (dt.Rows.Count == 1)
                    {
                        //if (dt.Rows[0]["StateName"].ToString() != "离职")
                        //{
                            txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();

                            lbDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            lbDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                            lbEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                        //}
                        //else
                        //    JSUtility.Alert(this, "该员工已经离职: " + empCode);
                    }
                    else
                    {
                        string info = "存在同名，请输入工号：\n\n";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            info += dt.Rows[i]["EmpCode"].ToString() + "  " + dt.Rows[i]["EmpName"].ToString() + "\n";
                        }
                        JSUtility.Alert(this, info);
                    }
                }
                else
                {
                    DataTable dt = employeeBiz.GetOneInfo(empCode);
                    if (dt.Rows.Count > 0)
                    {
                        //if (dt.Rows[0]["StateName"].ToString() != "离职")
                        //{
                            lbDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            lbDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                            lbEmpName.Text = dt.Rows[0]["EmpName"].ToString();

                        //}
                        //else
                        //    JSUtility.Alert(this, "该员工已经离职");
                    }
                }
            }
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }

        protected void ddlProCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            string proCode = ddlProCode.SelectedValue;
            if (proCode == "0") return;
            //get product info
            DataTable dt0 = jjProdBiz.GetOneInfo(proCode);
            if (dt0.Rows.Count > 0)
            {
                lbProName.Text = dt0.Rows[0]["Name"].ToString();
                lbProModel.Text = dt0.Rows[0]["ProModel"].ToString();
                lbTypeName.Text = dt0.Rows[0]["TypeName"].ToString();
                //get peocess info
                DataTable dt1 = jjProcBiz.GetLists(proCode, "Y");
                if (dt1.Rows.Count > 0)
                {
                    defDicBiz.InitDropDownList(dt1, ddlProcessCode);
                }
            }
        }

        protected void ddlProcessCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            string procCode = ddlProcessCode.SelectedValue;
            if (procCode == "0") return;
            DataTable dt1 = jjProcBiz.GetOneInfo(procCode);
            if (dt1.Rows.Count > 0)
            {
                lbProcName.Text = dt1.Rows[0]["Name"].ToString();
                txtPrice.Text = dt1.Rows[0]["Price"].ToString();
                txtScrapSal.Text = dt1.Rows[0]["ScrapSal"].ToString();
            }
        }

    

        protected void txtNum_TextChanged(object sender, EventArgs e)
        {
            string price = txtPrice.Text.Trim();
            string num = txtNum.Text.Trim();
            decimal n=decimal.Parse(price)*Int32.Parse(num);
            txtNumAmount.Text = n.ToString();
            txtJjSalary.Text = n.ToString();
        }

        protected void txtScrapNum_TextChanged(object sender, EventArgs e)
        {
            string num = txtScrapNum.Text.Trim();
            string price = txtScrapSal.Text.Trim();
            decimal n = decimal.Parse(price) * Int32.Parse(num);
            txtScrapAmount.Text = n.ToString();
            string m = txtNumAmount.Text.Trim();
            txtJjSalary.Text =(decimal.Parse(m)+n).ToString();
        }
    }
}